from django import template
from django.urls import reverse
from django.http.request import QueryDict

register = template.Library()


@register.simple_tag
def url_tag(name, request, *args, **kwargs):
    url = reverse(name, args=args, kwargs=kwargs)
    next_url = request.get_full_path()
    qd = QueryDict(mutable=True)
    qd['next'] = next_url
    return '{}?{}'.format(url, qd.urlencode())
